package cn.ym.org.service.impl;

import cn.ym.basic.exception.BusinessException;
import cn.ym.basic.util.BaiduAuditUtils;
import cn.ym.org.domain.Employee;
import cn.ym.org.domain.Shop;
import cn.ym.org.domain.ShopAuditLog;
import cn.ym.org.dto.ShopDto;
import cn.ym.org.mapper.EmployeeMapper;
import cn.ym.org.mapper.ShopAuditLogMapper;
import cn.ym.org.mapper.ShopMapper;
import cn.ym.org.service.IShopService;
import cn.ym.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private JavaMailSender javaMailSender;

    /**
     * 店铺入驻校验
     * @param shop
     */
    @Override
    public void settlement(Shop shop) {
        //一：
        // 1.校验：空值校验，前端校验容易被禁用掉，需要后端校验
        if (StringUtils.isEmpty(shop.getName())) {
            throw new BusinessException("店铺名称不能为空!!!");
        }

        if (StringUtils.isEmpty(shop.getAddress())) {
            throw new BusinessException("店铺地址不能为空!!!");
        }
        //2.1校验：百度AI智能审核
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("店铺名称不合法!!!");
        }
        //2.2如果你上传了图片 ，但是上传的图片不合规
        if(!StringUtils.isEmpty(shop.getLogo())
                && !BaiduAuditUtils.ImgCensor("http://123.207.27.208"+shop.getLogo())){
            throw new BusinessException("店铺LOGO不合法!!!");
        }

        //3.该店铺是否已经被入驻过 - 怎么判断入驻过？店铺名 + 地址
        Shop shopTemp =shopMapper.findByNameAndAddress(shop);
        if(shopTemp != null){
            throw new BusinessException("店铺已经入驻，请直接登陆！忘记密码请找回密码");
        }


        //二：添加到数据库
        //.先添加t_employee => 自增长id
        Employee employee = shop.getAdmin();
        employee.setState(0);//先设置为禁用，审核通过并激活成功之后设置为1
        employeeMapper.add(employee);

        //2.将自增长id设置到shop对象
        shop.setAdminId(employee.getId());
        //3.再添加t_shop => 自增长id
        shopMapper.add(shop);

        //4.将shop的自增长id设置到employee对象
        employee.setShop_id(shop.getId());
        //5.修改t_employee
        employeeMapper.updateById(employee);
    }



    /**
     *  审核成功发送邮件
     * @param log
     * @throws MessagingException
     */
    @Override
    public void pass(ShopAuditLog log) throws MessagingException {
        //1.改店铺状态：shop state 1 - 2
        shopMapper.updateState(log.getShopId(),2);
        //2.添加审核日志：
        log.setState(2);
        shopAuditLogMapper.add(log);
        //3.发送邮件：告诉店铺管理员激活邮件使用
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        //发送方
        helper.setFrom("210513347@qq.com");
        helper.setSubject("店铺激活邮件");
        //true - 能够再内容中编写html标签 - 会解析
        Shop shop = shopMapper.findOne(log.getShopId());
        helper.setText("<h1>你的店铺已经注册!!!</h1><img src='http://123.207.27.208"
                        +shop.getLogo()
                        +"' ><a href='http://127.0.0.1:8080/shop/active/"
                        +shop.getId()+"'>点击该链接激活</a>",
                true);
        Employee employee = employeeMapper.findOne(shop.getAdminId());
        //收件人
        helper.setTo(employee.getEmail());
        javaMailSender.send(mimeMessage);//发送
    }

    /**
     *  审核失败发送邮件
     * @param log
     * @throws MessagingException
     */
    @Override
    public void reject(ShopAuditLog log) throws MessagingException {
        //1.改店铺状态：shop state 1 - 4
        shopMapper.updateState(log.getShopId(),4);

        //2.添加审核日志
        log.setState(4);
        shopAuditLogMapper.add(log);

        //3.发送邮件：告诉店铺管理员激活邮件使用
        //创建复杂邮件对象
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //4.发送复杂邮件的工具类
        //true - 能够添加附件
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        //发送方
        helper.setFrom("210513347@qq.com");
        helper.setSubject("店铺审核失败");
        //true - 能够再内容中编写html标签 - 会解析
        Shop shop = shopMapper.findOne(log.getShopId());
        helper.setText("<h1>你的店铺入驻失败，请修改信息之后重新入驻</h1>",
                true);
        Employee employee = employeeMapper.findOne(shop.getAdminId());
        //收件人
        helper.setTo(employee.getEmail());
        javaMailSender.send(mimeMessage);
    }

    /**
     * 图形报表
     * @return
     */
    @Override
    public List<ShopDto> echarts() {
        return shopMapper.echarts();
    }


    /**
     * 店铺激活
     * @param id
     * @return
     */
    @Override
    public String active(Long id) {
        Shop shop = shopMapper.findOne(id);
        shop.setState(3);//激活=3
        shopMapper.updateById(shop);
        return "激活成功";
    }


}
